package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
@Slf4j
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    //新增检查项
    public void add(CheckItem checkItem) {
        log.debug("service:{"+checkItem+"}");
        checkItemDao.add(checkItem);

    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        log.debug("service: queryPageBean ==> " + queryPageBean);
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        Page<CheckItem> checkItemPage = checkItemDao.findByCondition(queryPageBean.getQueryString());
        log.debug("checkItemPage: ==>" + checkItemPage);

        return new PageResult(checkItemPage.getTotal(),checkItemPage);
    }

    @Override
    public void delById(Integer id) {
        long count = checkItemDao.findCountById(id);
        if (count>0){
            throw new RuntimeException("该检查项被检查组关联,不能删除!!");
        }else {
            checkItemDao.delById(id);
        }
    }

    @Override
    public CheckItem findById(Integer id) {
        log.debug("service: findById: id ==> " + id);
        CheckItem checkItem = checkItemDao.findById(id);
        log.debug("findById: checkItem: ===> " +checkItem);
        return checkItem;
    }

    @Override
    public void edit(CheckItem checkItem) {
        log.debug("service: edit: checkItem ==> " + checkItem);
        checkItemDao.edit(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        List<CheckItem> checkItemList = checkItemDao.findAll();
        log.debug("service: findAll: checkItemList: ===> " +checkItemList);
        return checkItemList;
    }
}
